package net.zjitc.dao;

import net.zjitc.Utils.C3P0Utils;
import net.zjitc.domain.Book;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/10/9.
 */
public class BookDaoImpl implements BookDao{
    @Override
    public List<Book> findAll() {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSourse());
        String sql="select * from book";
        try {
            List<Book> list= (List<Book>) runner.query(sql,new BeanListHandler(Book.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Book findById(String id) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDataSourse());
        String sql="select * from book where id=?";
        try {
            Book book= (Book) runner.query(sql,new BeanHandler(Book.class),id);
            return book;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
